from flask import Flask
from config import config_map
from flask_sqlalchemy import SQLAlchemy
import redis
from flask_wtf import  CSRFProtect
from flask_session import Session

#创建数据库对象

db = SQLAlchemy()
def create_app (config_name):
    app = Flask(__name__)
    #把配置加载到app对象
    # 获取配置文件里面的key
    name = config_map.get(config_name)
    app.config.from_object(name)
    #初始化
    db.init_app(app)
    #创建redis对象,用来保存验证码
    redis_store = redis.StrictRedis(host=name.REDIS_HOST,port=name.REDIS_PORT,)
    #开启CSRF保护,避免视图受到攻击
    CSRFProtect(app)
    #开启Session
    Session(app)
    #导入index蓝图并且激活
    from info.index import index_blue
    app.register_blueprint(index_blue)
    return app